package com.lt.test.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lt.test.api.CloudlingService;
import com.lt.test.provider.mapper.ChlChannelMapper;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.*;

@Service(version = "1.0.0")
public class CloudlingServiceImpl implements CloudlingService {
    @Autowired
    private ChlChannelMapper chlChannelMapper;

    private static final Logger log = LoggerFactory.getLogger(CloudlingServiceImpl.class);

    @Override
    public List<Map<String, Object>> findAllChlChannelKind() throws Exception {

        return chlChannelMapper.findAllChlChannelKind();
    }

    @Override
    public List<Map<String, Object>> findChlChannel(Long id, Long pId, Long cck) throws Exception {
        boolean flag = false;
        if (pId == 0) { // 树的根部
            id = 11520001L;
            flag = true;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("cck", cck);
        List<Map<String, Object>> result = chlChannelMapper.findChlChanne(params);
        if (flag) {
            for (Map<String, Object> map : result) {
                map.put("pid", 0);
            }
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> findAllChlChannel(Map params) throws Exception {
        List<Map<String, Object>> result = chlChannelMapper.findAllChlChanne(params);
        for (Map<String, Object> map : result) {
            if (map.get("pid").toString().equals("11520001")) {
                map.put("pid", -1);
            }
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> findUserIdToSendMessage(Map<String, Object> parameter) throws Exception {

        String model = parameter.get("model").toString();// 接受方
        String faSongFang = parameter.get("faSongFang").toString();// 发送方
        String quDaoIds = parameter.get("quDaoId") == null ? "" : parameter.get("quDaoId").toString();// d
        String fiveChannelCode = parameter.get("fiveChannelCode") == null ? "" : parameter.get("fiveChannelCode").toString();// d
        Long firstUserId = parameter.get("firstUserId") == null || parameter.get("firstUserId") .toString().equals("") ? 0 : Long.parseLong(parameter.get("firstUserId").toString());// 第一个用户Id
        Long endUserId = parameter.get("endUserId") == null || parameter.get("endUserId") .toString().equals("") ? 10000000 : Long.parseLong(parameter.get("endUserId").toString());// 最后一个用户Id


        List<String> cloudingIds;
        if (parameter.get("cloudId") == null || parameter.get("cloudId").toString().equals("")) {
            cloudingIds = chlChannelMapper.findAllCloudingId(faSongFang);
        }else {
            cloudingIds = new ArrayList<>();
            cloudingIds.add(parameter.get("cloudId").toString());
        }


        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, Object> useridAndcloudingIdparams = new HashedMap(2, 1.0F);
        useridAndcloudingIdparams.put("firstUserId", firstUserId);
        useridAndcloudingIdparams.put("endUserId", endUserId);
        useridAndcloudingIdparams.put("fiveChannelCode", fiveChannelCode);
        switch (model) {
            case "1":
                for (String cloudingId : cloudingIds) {
                    useridAndcloudingIdparams.put("cloudingId",Long.parseLong(cloudingId));
                    if (!quDaoIds.equals("") && !fiveChannelCode.equals("")){
                        useridAndcloudingIdparams.put("quDaoId", quDaoIds);
                        useridAndcloudingIdparams.put("cloudingId", cloudingId);
                        log.info("渠道发送，开始查询用户ID，参数："+useridAndcloudingIdparams);
                        List<String> users = chlChannelMapper.findAllUserIdByCloudingIdAndQuDaoId(useridAndcloudingIdparams);
                        if (users != null && !users.isEmpty()) {
                            Map<String, Object> cloudingAndUser = new HashMap<>();
                            cloudingAndUser.put("cloudingId", cloudingId);
                            cloudingAndUser.put("users", users);
                            result.add(cloudingAndUser);
                        }
                    }else {
                        List<String> users = chlChannelMapper.findAllUserIdByCloudingId(useridAndcloudingIdparams);
                        if (users != null && !users.isEmpty()) {
                            Map<String, Object> cloudingAndUser = new HashMap<>();
                            cloudingAndUser.put("cloudingId", cloudingId);
                            cloudingAndUser.put("users", users);
                            result.add(cloudingAndUser);
                        }
                    }
                }
                break;
            case "2":
                log.info("渠道ID===========" + parameter.get("quDaoId").toString());
                Long quDaoId = Long.valueOf(parameter.get("quDaoId").toString());
                switch (quDaoId.toString()) {
                    case "-1":
                        for (String cloudingId : cloudingIds) {
                            useridAndcloudingIdparams.put("cloudingId",Long.parseLong(cloudingId));
                            if (!quDaoIds.equals("") && !fiveChannelCode.equals("")){
                                useridAndcloudingIdparams.put("quDaoId", quDaoIds);
                                useridAndcloudingIdparams.put("cloudingId", cloudingId);
                                log.info("渠道发送，开始查询用户ID，参数："+useridAndcloudingIdparams);
                                List<String> users = chlChannelMapper.findAllUserIdByCloudingIdAndQuDaoId(useridAndcloudingIdparams);
                                if (users != null && !users.isEmpty()) {
                                    Map<String, Object> cloudingAndUser = new HashMap<>();
                                    cloudingAndUser.put("cloudingId", cloudingId);
                                    cloudingAndUser.put("users", users);
                                    result.add(cloudingAndUser);
                                }
                            }else {
                                List<String> users = chlChannelMapper.findAllUserIdByCloudingId(useridAndcloudingIdparams);
                                if (users != null && !users.isEmpty()) {
                                    Map<String, Object> cloudingAndUser = new HashMap<>();
                                    cloudingAndUser.put("cloudingId", cloudingId);
                                    cloudingAndUser.put("users", users);
                                    result.add(cloudingAndUser);
                                }
                            }
                        }
                        break;
                    case "-2":
                    case "-3":
                        return result;
                    default:
                        for (String cloudingId : cloudingIds) {
                            Map<String, Object> params = new HashMap<>();
                            params.put("cloudingId", cloudingId);
                            params.put("quDaoId", quDaoId);
                            params.put("firstUserId",firstUserId);
                            params.put("endUserId", endUserId);
                            log.info("渠道发送，开始查询用户ID，参数："+params);
                            List<String> users = chlChannelMapper.findAllUserIdByCloudingIdAndQuDaoId(params);
                            if (users != null && !users.isEmpty()) {
                                Map<String, Object> cloudingAndUser = new HashMap<>();
                                cloudingAndUser.put("cloudingId", cloudingId);
                                cloudingAndUser.put("users", users);
                                result.add(cloudingAndUser);
                            }
                        }
                        break;
                }
                break;
            case "3":
                for (String cloudingId : cloudingIds) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("cloudingId", cloudingId);
                    params.put("userType", parameter.get("roleId").toString());
                    List<String> users = chlChannelMapper.findAllUserIdByCloudingIdAndUserType(params);
                    if (users != null && !users.isEmpty()) {
                        Map<String, Object> cloudingAndUser = new HashMap<>();
                        cloudingAndUser.put("cloudingId", cloudingId);
                        cloudingAndUser.put("users", users);
                        result.add(cloudingAndUser);
                    }
                }
                break;
            case "4":
                Map<String, String> params = new HashMap<>(2, 1.0F);
                String phones = parameter.get("phone").toString();
                String[] phoneArray = phones.split(",");
                log.info(Arrays.toString(phoneArray));
                params.put("faSongFang", faSongFang);
                for (String phone : phoneArray) {
                    params.put("phone", phone);
                    List<Map<String, String>> userIdAndCloudingIdList = chlChannelMapper.findUserIdAndCloudingIdByPhone(params);
                    if (userIdAndCloudingIdList.size() != 0) {
                        //多小云修改
                        Map<String, String> userIdAndCloudingId = userIdAndCloudingIdList.get(0);
                        String userId = userIdAndCloudingId.get("userId");
                        Map<String, Object> cloudingAndUser = new HashMap<>();
                        cloudingAndUser.put("cloudingId", userIdAndCloudingId.get("cloudingId"));
                        List<String> users = new ArrayList<>();
                        users.add(userId);
                        cloudingAndUser.put("users", users);
                        result.add(cloudingAndUser);
                    }

                }
                break;
            default:
                break;
        }
        return result;
    }


    @Override
    public Boolean insertCloudMessageRecord(Map<String, Object> cloudMessage) {

        try {
            chlChannelMapper.insertCloudMessageRecord(cloudMessage);
            log.info("插入小云消息成功");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("插入小云消息失败");
            return false;
        }
    }
}
